package com.hhh.springai_test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhh.springai_test.common.ErrorCode;
import com.hhh.springai_test.exception.BusinessException;
import com.hhh.springai_test.model.dto.communityCategory.AddCommunityCategoryDTO;
import com.hhh.springai_test.model.dto.communityCategory.UpdateCommunityCategoryDTO;
import com.hhh.springai_test.model.po.CommunityCategory;
import com.hhh.springai_test.service.CommunityCategoryService;
import com.hhh.springai_test.mapper.CommunityCategoryMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author 29915
* @description 针对表【community_category(社区分类表)】的数据库操作Service实现
* @createDate 2025-02-02 11:00:56
*/
@Service
public class CommunityCategoryServiceImpl extends ServiceImpl<CommunityCategoryMapper, CommunityCategory>
    implements CommunityCategoryService{

    @Override
    public Long addCategory(AddCommunityCategoryDTO addCommunityCategoryDTO) {
        if (addCommunityCategoryDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        if (StringUtils.isAnyBlank(addCommunityCategoryDTO.getCategorycode(), 
                addCommunityCategoryDTO.getCategorynamezh())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "分类编码或名称不能为空");
        }
        QueryWrapper<CommunityCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("categorycode", addCommunityCategoryDTO.getCategorycode());
        long count = this.count(queryWrapper);
        if (count > 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "分类编码已存在");
        }
        CommunityCategory category = new CommunityCategory();
        BeanUtils.copyProperties(addCommunityCategoryDTO, category);
        boolean result = this.save(category);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "添加失败");
        }
        return category.getId();
    }

    @Override
    public boolean deleteCategory(Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "分类ID不合法");
        }
        QueryWrapper<CommunityCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        if (this.count(queryWrapper) == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "分类不存在");
        }
        return this.removeById(id);
    }

    @Override
    public boolean updateCategory(UpdateCommunityCategoryDTO updateCommunityCategoryDTO) {
        if (updateCommunityCategoryDTO == null || updateCommunityCategoryDTO.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数错误");
        }
        QueryWrapper<CommunityCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("categorycode", updateCommunityCategoryDTO.getCategorycode());
        long count = this.count(queryWrapper);
        if (count > 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "分类编码已存在");
        }
        CommunityCategory category = new CommunityCategory();
        BeanUtils.copyProperties(updateCommunityCategoryDTO, category);
        category.setUpdatetime(new Date());
        
        return this.updateById(category);
    }

    @Override
    public List<CommunityCategory> listCategories() {
        return this.list();
    }
}




